home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Everything / EditText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  9.8 KB  |  435 lines  |  [TEXT/CWIE]

  1. /* EditText.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15. #include <stdlib.h>
  16.  
  17. #include "Globals.h"
  18. #include "ResourceDefs.h"
  19. #include "DoScrap.h"
  20. #include "Miscellany.h"
  21. #include "Scrolling.h"
  22. #include "ControlUtils.h"
  23. #include "Dispatcher.h"
  24. #include "DDocData.h"
  25. #include "EverythingEngine.h"
  26. #include "EverythingDoc.h"
  27. #include "EditText.h"
  28.  
  29.  
  30. static    EverythingEngine*      GetEngine    (EditText*    self);
  31.  
  32. static    void    DoUndo        (EditText*    self);
  33. static    void    DoCut        (EditText*    self);
  34. static    void    DoCopy        (EditText*    self);
  35. static    void    DoPaste        (EditText*    self);
  36. static    void    DoClear        (EditText*    self);
  37. static    void    DoSelectAll        (EditText*    self);
  38. static    void    DoShowClipboard    (EditText*    self);
  39.  
  40.  
  41. //----------
  42. EditText*        NewEditText ()
  43. {
  44.     EditText*        window;
  45.  
  46.     window = (EditText*)malloc (sizeof (EditText));
  47.     EditText_Init (window);
  48.     SetClassID (window, classEditText);
  49.  
  50.     return window;
  51. }
  52.  
  53. //----------
  54. void    DeleteEditText (
  55.     EditText*        window)
  56. {
  57.     EditText_Free (window);
  58.     free (window);
  59. }
  60.  
  61. //----------
  62. void    EditText_Create (
  63.     AMDoc*            inDoc,
  64.     DDocData*        inData)
  65. {
  66.     EditText*        winObj = NewEditText ();
  67.  
  68.     if (winObj != nil) {
  69.         EditText_Open (winObj, inDoc, inData);
  70.     }
  71. }
  72.  
  73. //----------
  74. void    EditText_Init (
  75.     EditText*        self)
  76. {
  77. }
  78.  
  79. //----------
  80. void    EditText_Free (
  81.     EditText*        self)
  82. {
  83. }
  84.  
  85. //----------
  86. EverythingEngine*    GetEngine (
  87.     EditText*        self)
  88. {
  89.     return (EverythingEngine*) self->super.mDoc->mEngine;
  90. }
  91.  
  92. /*----------*/
  93. void    EditText_Open (
  94.     EditText*        self,
  95.     AMDoc*            inDoc,
  96.     DDocData*        inData)
  97. {
  98.     WindowPtr        window;
  99.     Handle            wftb;
  100.  
  101.     self->super.mDoc = inDoc;
  102.     self->mData = inData;
  103.     AddResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  104.  
  105.     window = GetNewCWindow (WIND_EditText, nil, (WindowPtr) -1L);
  106.     if (AMEngine_GetFilename (self->super.mDoc->mEngine) [0] != 0) {
  107.         SetWTitle (window, AMEngine_GetFilename (self->super.mDoc->mEngine));
  108.     }
  109.     self->super.mWindow = window;
  110.     ((EverythingDoc*)self->super.mDoc)->mEditTextPtr = window;
  111.  
  112.     SetWindowKind (window, 'AM');
  113.     SetWRefCon (window, (long) self);
  114.     SetPort (window);
  115.     SetInfo (window);
  116.  
  117.     wftb = GetResource ('Wftb', WIND_EditText);
  118.  
  119.     CreateRootControl (window, &self->super.mRootControl);
  120.  
  121.     self->super.vScroll = nil;
  122.     self->super.hScroll = nil;
  123.  
  124.  
  125.     self->mSmallHandle = GetNewControl (CNTL_Small, window);
  126.     SetWindowItemFont (self->mSmallHandle, wftb, 1);
  127.     SetControlTextStr (self->mSmallHandle, GetSmall (self->mData));
  128.  
  129.     self->mLargeHandle = GetNewControl (CNTL_Large, window);
  130.     SetWindowItemFont (self->mLargeHandle, wftb, 2);
  131.     SetControlTextStr (self->mLargeHandle, GetLarge (self->mData));
  132.  
  133.     self->mX12345Handle = GetNewControl (CNTL_X12345, window);
  134.     SetWindowItemFont (self->mX12345Handle, wftb, 3);
  135.     SetControlTextValue (self->mX12345Handle, GetX12345 (self->mData));
  136.  
  137.     self->mX12345e6Handle = GetNewControl (CNTL_X12345e6, window);
  138.     SetWindowItemFont (self->mX12345e6Handle, wftb, 4);
  139.     SetControlTextFloat (self->mX12345e6Handle, GetX12345e6 (self->mData));
  140.  
  141.     self->mPasswordHandle = GetNewControl (CNTL_Password, window);
  142.     SetWindowItemFont (self->mPasswordHandle, wftb, 5);
  143.     SetControlTextStr (self->mPasswordHandle, GetPassword (self->mData));
  144.  
  145.     self->mDateHandle = GetNewControl (CNTL_Date, window);
  146.     SetWindowItemFont (self->mDateHandle, wftb, 6);
  147.     SetClockDateTime (self->mDateHandle, GetTheDate (self->mData));
  148.  
  149.     self->mTimeHandle = GetNewControl (CNTL_Time, window);
  150.     SetWindowItemFont (self->mTimeHandle, wftb, 7);
  151.     SetClockDateTime (self->mTimeHandle, GetTheTime (self->mData));
  152.  
  153.     self->mStyledHandle = GetNewControl (CNTL_Styled, window);
  154.     SetWindowItemFont (self->mStyledHandle, wftb, 8);
  155.     SetControlTextStr (self->mStyledHandle, GetStyled (self->mData));
  156.  
  157.     AdvanceKeyboardFocus (window);
  158.  
  159.     ShowWindow (window);
  160. }
  161.  
  162. /*----------*/
  163. void    EditText_Close (
  164.     EditText*        self)
  165. {
  166.     RemoveResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  167.  
  168.     ((EverythingDoc*)self->super.mDoc)->mEditTextPtr = nil;
  169.     SetInfo (nil);
  170.     HideWindow (self->super.mWindow);
  171.     DisposeWindow (self->super.mWindow);
  172.  
  173.     DeleteEditText (self);
  174. }
  175.  
  176. /*----------*/
  177. void    EditText_Track (
  178.     EditText*        self,
  179.     ControlHandle    whichControl,
  180.     short            whichPart,
  181.     Point            where)
  182. {
  183.     Rect            bounds;
  184.     short            newValue;
  185.  
  186.     if (whichControl == self->mSmallHandle) {
  187.         AMWindow_HandleEditClick ((AMWindow*) self, self->mSmallHandle, where);
  188.     }
  189.     if (whichControl == self->mLargeHandle) {
  190.         AMWindow_HandleEditClick ((AMWindow*) self, self->mLargeHandle, where);
  191.     }
  192.     if (whichControl == self->mX12345Handle) {
  193.         AMWindow_HandleEditClick ((AMWindow*) self, self->mX12345Handle, where);
  194.     }
  195.     if (whichControl == self->mX12345e6Handle) {
  196.         AMWindow_HandleEditClick ((AMWindow*) self, self->mX12345e6Handle, where);
  197.     }
  198.     if (whichControl == self->mPasswordHandle) {
  199.         AMWindow_HandleEditClick ((AMWindow*) self, self->mPasswordHandle, where);
  200.     }
  201.     if (whichControl == self->mDateHandle) {
  202.         AMWindow_HandleEditClick ((AMWindow*) self, self->mDateHandle, where);
  203.     }
  204.     if (whichControl == self->mTimeHandle) {
  205.         AMWindow_HandleEditClick ((AMWindow*) self, self->mTimeHandle, where);
  206.     }
  207.     if (whichControl == self->mStyledHandle) {
  208.         AMWindow_HandleEditClick ((AMWindow*) self, self->mStyledHandle, where);
  209.     }
  210. }
  211.  
  212. //----------
  213. void    EditText_DataChanged (
  214.     EditText*        self,
  215.     long            inDataID)
  216. {
  217.     if (inDataID == idSmall) {
  218.         SetControlTextStr (self->mSmallHandle, GetSmall (self->mData));
  219.     }
  220.     if (inDataID == idLarge) {
  221.         SetControlTextStr (self->mLargeHandle, GetLarge (self->mData));
  222.     }
  223.     if (inDataID == idX12345) {
  224.         SetControlTextValue (self->mX12345Handle, GetX12345 (self->mData));
  225.     }
  226.     if (inDataID == idX12345e6) {
  227.         SetControlTextFloat (self->mX12345e6Handle, GetX12345e6 (self->mData));
  228.     }
  229.     if (inDataID == idPassword) {
  230.         SetControlTextStr (self->mPasswordHandle, GetPassword (self->mData));
  231.     }
  232.     if (inDataID == idTheDate) {
  233.         SetClockDateTime (self->mDateHandle, GetTheDate (self->mData));
  234.     }
  235.     if (inDataID == idTheTime) {
  236.         SetClockDateTime (self->mTimeHandle, GetTheTime (self->mData));
  237.     }
  238.     if (inDataID == idStyled) {
  239.         SetControlTextStr (self->mStyledHandle, GetStyled (self->mData));
  240.     }
  241. }
  242.  
  243. /*----------*/
  244. void    EditText_MouseIn (
  245.     EditText*        self,
  246.     Point            where,
  247.     short            modifiers)
  248. {
  249.     Rect        bounds;
  250.  
  251. }
  252.  
  253. //----------
  254. void    EditText_ExitCurField (
  255.     EditText*        self)
  256. {
  257.     ControlHandle    focus;
  258.  
  259.     GetKeyboardFocus (self->super.mWindow, &focus);
  260.  
  261.     if (focus == nil) {
  262.         // nothing to exit
  263.  
  264.     } else if (focus == self->mSmallHandle) {
  265.         SetSmallHandle (self->mData, GetEditTextChars (self->mSmallHandle));
  266.     } else if (focus == self->mLargeHandle) {
  267.         SetLargeHandle (self->mData, GetEditTextChars (self->mLargeHandle));
  268.     } else if (focus == self->mX12345Handle) {
  269.         SetX12345 (self->mData, GetControlTextValue (self->mX12345Handle));
  270.     } else if (focus == self->mX12345e6Handle) {
  271.         SetX12345e6 (self->mData, GetControlTextFloat (self->mX12345e6Handle));
  272.     } else if (focus == self->mPasswordHandle) {
  273.         SetPasswordHandle (self->mData, GetEditTextChars (self->mPasswordHandle));
  274.     } else if (focus == self->mDateHandle) {
  275.         SetTheDate (self->mData, GetClockDateTime (self->mDateHandle));
  276.     } else if (focus == self->mTimeHandle) {
  277.         SetTheTime (self->mData, GetClockDateTime (self->mTimeHandle));
  278.     } else if (focus == self->mStyledHandle) {
  279.         SetStyledHandle (self->mData, GetEditTextChars (self->mStyledHandle));
  280.     }
  281. }
  282.  
  283. /*----------*/
  284. void    EditText_TypeIn (
  285.     EditText*        self,
  286.     char            ch)
  287. {
  288.     ControlHandle    focus;
  289.     short            keyCode;
  290.  
  291.     GetKeyboardFocus (self->super.mWindow, &focus);
  292.  
  293.     if ((ch == charEnter)
  294.     ||  (ch == charReturn)) {
  295.         EditText_ExitCurField (self);    // Dispatch
  296.     } else if (ch == charEsc) {
  297.     } else if (ch == charTab) {
  298.         AMWindow_DoTab ((AMWindow*) self, (curEvent.modifiers & optionKey) != 0);
  299.     } else if (focus != nil) {
  300.         keyCode = curEvent.message & keyCodeMask;
  301.         HandleControlKey (focus, keyCode, ch, curEvent.modifiers);
  302.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  303.     } else {
  304.         SysBeep (1);
  305.     }
  306. }
  307.  
  308. /*----------*/
  309. void    EditText_Resize (
  310.     EditText*        self)
  311. {
  312.     /* application-specific code to resize items in window */
  313. }
  314.  
  315. /*----------*/
  316. void    EditText_Scroll (
  317.     EditText*        self,
  318.     short            newValue,
  319.     short            oldValue)
  320. {
  321.     /* application-specific code to scroll window */
  322.     if (gWhichScroll == self->super.vScroll) {
  323.     } else {    // horizontal
  324.     }
  325. }
  326.  
  327. //----------
  328. void    DoUndo (
  329.     EditText*        self)
  330. {
  331. } // DoUndo
  332.  
  333. //----------
  334. void    DoCut (
  335.     EditText*        self)
  336. {
  337.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  338.  
  339.     if (curTE != nil) {
  340.         TECut (curTE);
  341.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  342.         scrapDirty = true;
  343.     }
  344. } // DoCut
  345.  
  346. //----------
  347. void    DoCopy (
  348.     EditText*        self)
  349. {
  350.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  351.  
  352.     if (curTE != nil) {
  353.         TECopy (curTE);
  354.         scrapDirty = true;
  355.     }
  356. } // DoCopy
  357.  
  358. //----------
  359. void    DoPaste (
  360.     EditText*        self)
  361. {
  362.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  363.  
  364.     if (curTE != nil) {
  365.         TEPaste (curTE);
  366.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  367.     }
  368. } // DoPaste
  369.  
  370. //----------
  371. void    DoClear (
  372.     EditText*        self)
  373. {
  374.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  375.  
  376.     if (curTE != nil) {
  377.         TEDelete (curTE);
  378.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  379.     }
  380. } // DoClear
  381.  
  382. //----------
  383. void    DoSelectAll (
  384.     EditText*        self)
  385. {
  386.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  387.  
  388.     if (curTE != nil) {
  389.         TESetSelect (0, 32767, curTE);
  390.     }
  391. } // DoSelectAll
  392.  
  393. //----------
  394. void    DoShowClipboard (
  395.     EditText*        self)
  396. {
  397. } // DoShowClipboard
  398.  
  399. //----------
  400. Boolean        EditText_DoCommand (
  401.     EditText*        self,
  402.     long            inCommand)
  403. {
  404.     Boolean        result = true;
  405.  
  406.     switch (inCommand) {
  407.         case cmdUndo:
  408.                 DoUndo (self);
  409.             break;
  410.         case cmdCut:
  411.                 DoCut (self);
  412.             break;
  413.         case cmdCopy:
  414.                 DoCopy (self);
  415.             break;
  416.         case cmdPaste:
  417.                 DoPaste (self);
  418.             break;
  419.         case cmdClear:
  420.                 DoClear (self);
  421.             break;
  422.         case cmdSelectAll:
  423.                 DoSelectAll (self);
  424.             break;
  425.         case cmdShowClipboard:
  426.                 DoShowClipboard (self);
  427.             break;
  428.  
  429.         default:
  430.                 result = false;
  431.     } // switch
  432.  
  433.     return result;
  434. }
  435.